<template>
  <div>
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader :addTodo="addTodo" />
        <!-- (1) 加冒号是因为想要传入js表达式，而非简单的字符串 -->
        <MyList :todos="todos" />
        <MyFooter
          :todos="todos"
          :checkAllTodo="checkAllTodo"
          :clearAllTodo="clearAllTodo"
        />
      </div>
    </div>
  </div>
</template>

<script>
// (8) 第三方库一般放在最上面，这是约定成俗的规则
import pubsub from "pubsub-js";
import MyHeader from "./components/MyHeader.vue";
import MyList from "./components/MyList.vue";
import MyFooter from "./components/MyFooter.vue";

export default {
  name: "App",
  components: {
    MyHeader,
    MyList,
    MyFooter,
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem("todos")) || [],
      // /**
      //  * (2) 有一堆数据用数组，然后每条数据复杂用对象
      //  * (3) id一般用字符串，因为数字有上限，字符串没有
      //  * */
      // { id: "001", title: "编程", done: false },
      // { id: "002", title: "唱歌", done: false },
      // { id: "003", title: "学习", done: false },
    };
  },
  methods: {
    addTodo(todoObj) {
      // (3) 我靠，如果不用数组的方法unshift，那么直接数组等于空
      this.todos.unshift(todoObj);
    },
    /**
     * (4) 希望在切换选中的时候能够反应到数据上(与done属性绑定)
     *     但是不能直接传给MyItem组件使用，现在使用麻烦的方法：先传给MyList再传给MyItem
     * (9) 通常用下划线_来当作占位符(没有实际作用，仅仅是占着)
     * */
    checkTodo(_, id) {
      // (5) 原来foreach是这么用的：数据源.foEeach((单个数据)=>{条件判断})
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    deleteTodo(_, id) {
      // (6) 原来过滤器里面的判断是针对留下来的数据，而不是对过滤的数据判断
      this.todos = this.todos.filter((todo) => todo.id !== id);
    },
    checkAllTodo(done) {
      this.todos.forEach((todo) => {
        todo.done = done;
      });
    },
    clearAllTodo() {
      this.todos = this.todos.filter((todo) => {
        return !todo.done;
      });
    },
    updateTodo(id, title) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.title = title;
      });
    },
  },
  watch: {
    todos: {
      deep: true,
      handler(todo) {
        /**
         * (1) 说到底，要么是对todos的添加要么是删除，然后再更新localStorage
         *     可是原来添加和删除操作都可以看作是todos的值的变化，只要是根据某值得变化
         *     而做出相应操作，那么就可以使用监视watch
         */
        localStorage.setItem("todos", JSON.stringify(todo));
      },
    },
  },
  mounted() {
    //  this.$bus.$on('checkTodo',this.checkTodo)
    //  this.$bus.$on('deleteTodo',this.deleteTodo)
    this.$bus.$on("updateTodo", this.updateTodo);
    this.checkTodoId = pubsub.subscribe("checkTodo", this.checkTodo);
    this.deleteTodoId = pubsub.subscribe("deleteTodo", this.deleteTodo);
    
  },
  beforeUnmount(){
    //  this.$bus.$off('checkTodo')
    //  this.$bus.$off('deleteTodo')
    this.$bus.$off('updateTodo');
    pubsub.unsubscribe(this.checkTodoId);
    pubsub.unsubscribe(this.deleteTodoId);
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}
.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
  display: none;
}
.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(85, 132, 150);
  margin-right: 5px;
  display: none;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn:focus {
  outline: none;
}
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
